package sdkshell.kuaiyan.com.kuaiyansdk;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.wmcsk.KuaiyanAppDataStatisticsSdk;
import com.wmcsk.KuaiyanDataStatisticsSdk;
import com.wmcsk.KuaiyanMenuWealSdk;
import com.wmcsk.KuaiyanSdk;
import com.wmcsk.KuaiyanSelectShareSdk;
import com.wmcsk.KuaiyanSignSdk;
import com.wmcsk.KuaiyanVideoShadowSdk;
import com.wmcsk.dl.bean.SDKConfig;
import com.wmcsk.dl.dl.internal.DLPluginManager;
import com.wmcsk.dl.listener.PluginAppDataStatisticsInitOkListener;
import com.wmcsk.dl.listener.PluginDataStatisticsInitOkListener;
import com.wmcsk.dl.listener.PluginInitOkListener;
import com.wmcsk.dl.listener.PluginMenuInitOkListener;
import com.wmcsk.dl.listener.PluginSelectShareInitOkListener;
import com.wmcsk.dl.listener.PluginSignInitOkListener;
import com.wmcsk.dl.listener.PluginVideoShadowInitOkListener;
import com.wmcsk.dl.util.LogUtils;
import com.wmcsk.interfaces.KYSdkInterface;
import com.wmcsk.interfaces.KuaiyanAppDataStatisticsInterface;
import com.wmcsk.interfaces.KuaiyanDataStatisticsInterface;
import com.wmcsk.interfaces.KuaiyanMenuSdkInterface;
import com.wmcsk.interfaces.KuaiyanSelectShareSdkInterface;
import com.wmcsk.interfaces.KuaiyanSignSdkInterface;
import com.wmcsk.interfaces.KuaiyanVideoShadowSdkInterface;

import java.util.List;

import sdkshell.kuaiyan.com.kuaiyansdk.bean.ApkNames;
import sdkshell.kuaiyan.com.kuaiyansdk.init.MenifestChecker;
import sdkshell.kuaiyan.com.kuaiyansdk.listener.KuaiyanSdkInitListener;
import sdkshell.kuaiyan.com.kuaiyansdk.manager.KyInterfacePull;
import sdkshell.kuaiyan.com.kuaiyansdk.manager.LocalPluginMager;

public class SdkInit {
	public static Context getContext() {
		return sdkContext;
	}
	private static Context sdkContext;
	public static Handler uiHander;
    private static SDKConfig sdkConfig;
    public static String appKeys;
    private static boolean hasInit = false;
    static {
        uiHander = new Handler(Looper.getMainLooper());
    }
	public static Handler getUiHandle() {
		return uiHander;
	}

    public static SDKConfig getSdkConfig() {
        return sdkConfig;
    }

    public static void init(Context context, final SDKConfig sdkConfig, final KuaiyanSdkInitListener kuaiyanSdkInitListener){
        if(!BuildConfig.isWeb) {
            MenifestChecker.checkMenifest(context);
        }
        String name = ApkNames.AdSdk;
        if(ApkNames.inited(name)){
            return;
        }
        ApkNames.inited.put(name,true);
        SdkInit.sdkConfig = sdkConfig;
        if(sdkConfig!=null){
            appKeys = sdkConfig.getAppKey();
        }
        if(!BuildConfig.isWeb) {
            sdkContext = context.getApplicationContext();
        }else{
            sdkContext = context;
        }
        PluginInitOkListener pluginInitOkListener =  new PluginInitOkListener() {
            @Override
            public void initOk(KYSdkInterface kySdkInterface) {
                initComplate(kySdkInterface,kuaiyanSdkInitListener);
            }

            @Override
            public void initError(KYSdkInterface kySdkInterface,String msg) {
                if(kySdkInterface!=null) {
                    initComplate(kySdkInterface, kuaiyanSdkInitListener);
                }else{
                    if(kuaiyanSdkInitListener!=null){
                        kuaiyanSdkInitListener.onCoreInitError(msg);
                    }
                }
            }
            @Override
            public SDKConfig getConfig() {
                return sdkConfig;
            }
            private void initComplate(KYSdkInterface kySdkInterface,KuaiyanSdkInitListener kuaiyanSdkInitListener){
                LogUtils.getNewInstance("KyInterfacePull").dob("初始化完成");
                KuaiyanSdk.getInstance().initKYSdkInterface(kySdkInterface);
                if(!TextUtils.isEmpty(SdkInit.appKeys)){
                    KuaiyanSdk.getInstance().setAppKye(SdkInit.appKeys);
                }
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitFinished();
                }
            }
        };
        LocalPluginMager.getInstance().copyAssertPlugin(name);//对比assert目录，本地插件目录以及网络下载目录
        if(!TextUtils.isEmpty(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath)) {
           DLPluginManager.getInstance(sdkContext).loadApk(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath);
           KyInterfacePull.getInstance().init(name,pluginInitOkListener);
        }
       checkUpdate(context,name);
	}

	public static void initKuaiyanVideoShadowSdk(Context context, final SDKConfig sdkConfig, final KuaiyanSdkInitListener kuaiyanSdkInitListener){
        MenifestChecker.checkMenifest(context);
        String name = ApkNames.videoShadowSdk;
        if(ApkNames.inited(name)){
            return;
        }
        ApkNames.inited.put(name,true);
        SdkInit.sdkConfig = sdkConfig;
        if(sdkConfig!=null){
            appKeys = sdkConfig.getAppKey();
        }
        sdkContext = context.getApplicationContext();
        PluginVideoShadowInitOkListener pluginVideoShadowInitOkListener = new PluginVideoShadowInitOkListener() {
            @Override
            public void initOk(KuaiyanVideoShadowSdkInterface kuaiyanSignSdkInterface) {
                initComplate(kuaiyanSignSdkInterface,kuaiyanSdkInitListener);
            }

            @Override
            public void initError(KuaiyanVideoShadowSdkInterface kuaiyanSignSdkInterface, String msg) {
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitError(msg);
                }
            }

            @Override
            public SDKConfig getConfig() {
                return sdkConfig;
            }
            private void initComplate(KuaiyanVideoShadowSdkInterface kySdkInterface,KuaiyanSdkInitListener kuaiyanSdkInitListener){
                LogUtils.getNewInstance("KyInterfacePull").dob("初始化完成");
                KuaiyanVideoShadowSdk.getInstance().initKYSdkInterface(kySdkInterface);
                if(!TextUtils.isEmpty(SdkInit.appKeys)){
                    KuaiyanSdk.getInstance().setAppKye(SdkInit.appKeys);
                }
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitFinished();
                }
            }
        };
        LocalPluginMager.getInstance().copyAssertPlugin(name);
        if(!TextUtils.isEmpty(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath)) {
            DLPluginManager.getInstance(sdkContext).loadApk(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath);
            KyInterfacePull.getInstance().init(name,pluginVideoShadowInitOkListener);
        }
        checkUpdate(context,name);
    }

	public static void initSignSdk(Context context, final SDKConfig sdkConfig, final KuaiyanSdkInitListener kuaiyanSdkInitListener){
        MenifestChecker.checkMenifest(context);
        String name = ApkNames.SignSdk;
        if(ApkNames.inited(name)){
            return;
        }
        ApkNames.inited.put(name,true);
        SdkInit.sdkConfig = sdkConfig;
        if(sdkConfig!=null){
            appKeys = sdkConfig.getAppKey();
        }
        sdkContext = context.getApplicationContext();
        PluginSignInitOkListener pluginInitOkListener =  new PluginSignInitOkListener() {
            @Override
            public void initOk(KuaiyanSignSdkInterface kySdkInterface) {
                initComplate(kySdkInterface,kuaiyanSdkInitListener);
            }

            @Override
            public void initError(KuaiyanSignSdkInterface kySdkInterface, String msg) {
                initComplate(kySdkInterface,kuaiyanSdkInitListener);
            }
            @Override
            public SDKConfig getConfig() {
                return sdkConfig;
            }
            private void initComplate(KuaiyanSignSdkInterface kySdkInterface,KuaiyanSdkInitListener kuaiyanSdkInitListener){
                LogUtils.getNewInstance("KyInterfacePull").dob("初始化完成");
                KuaiyanSignSdk.getInstance().initKYSdkInterface(kySdkInterface);
                if(!TextUtils.isEmpty(SdkInit.appKeys)){
                    KuaiyanSdk.getInstance().setAppKye(SdkInit.appKeys);
                }
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitFinished();
                }
            }
        };
        LocalPluginMager.getInstance().copyAssertPlugin(name);//对比assert目录，本地插件目录以及网络下载目录
        if(!TextUtils.isEmpty(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath)) {
            DLPluginManager.getInstance(sdkContext).loadApk(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath);
            KyInterfacePull.getInstance().init(name,pluginInitOkListener);
        }
        checkUpdate(context,name);
    }
    public static void initSelectShareSdk(Context context, final SDKConfig sdkConfig, final KuaiyanSdkInitListener kuaiyanSdkInitListener){
        MenifestChecker.checkMenifest(context);
        String name = ApkNames.ShareSdk;
        if(ApkNames.inited(name)){
            return;
        }
        ApkNames.inited.put(name,true);
        SdkInit.sdkConfig = sdkConfig;
        if(sdkConfig!=null){
            appKeys = sdkConfig.getAppKey();
        }
        sdkContext = context.getApplicationContext();
        PluginSelectShareInitOkListener pluginInitOkListener =  new PluginSelectShareInitOkListener() {
            @Override
            public void initOk(KuaiyanSelectShareSdkInterface kuaiyanSelectShareSdkInterface) {
                initComplate(kuaiyanSelectShareSdkInterface,kuaiyanSdkInitListener);
            }

            @Override
            public void initError(KuaiyanSelectShareSdkInterface kuaiyanSelectShareSdkInterface, String msg) {
                initComplate(kuaiyanSelectShareSdkInterface,kuaiyanSdkInitListener);
            }
            @Override
            public SDKConfig getConfig() {
                return sdkConfig;
            }
            private void initComplate(KuaiyanSelectShareSdkInterface kuaiyanSelectShareSdkInterface,KuaiyanSdkInitListener kuaiyanSdkInitListener){
                LogUtils.getNewInstance("KuaiyanSelectShareSdkInterface").dob("初始化完成"+kuaiyanSelectShareSdkInterface.getClass());
                KuaiyanSelectShareSdk.getInstance().initKYSdkInterface(kuaiyanSelectShareSdkInterface);
                if(!TextUtils.isEmpty(SdkInit.appKeys)){
                    KuaiyanSdk.getInstance().setAppKye(SdkInit.appKeys);
                }
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitFinished();
                }
            }
        };
        LocalPluginMager.getInstance().copyAssertPlugin(name);//对比assert目录，本地插件目录以及网络下载目录
        if(!TextUtils.isEmpty(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath)) {
            DLPluginManager.getInstance(sdkContext).loadApk(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath);
            KyInterfacePull.getInstance().init(name,pluginInitOkListener);
        }
        checkUpdate(context,name);
    }
    public static void initMenuWealSdk(Context context, final SDKConfig sdkConfig, final KuaiyanSdkInitListener kuaiyanSdkInitListener){
        MenifestChecker.checkMenifest(context);
        String name = ApkNames.MenuWealSdk;
        if(ApkNames.inited(name)){
            return;
        }
        ApkNames.inited.put(name,true);
        SdkInit.sdkConfig = sdkConfig;
        if(sdkConfig!=null){
            appKeys = sdkConfig.getAppKey();
        }
        sdkContext = context.getApplicationContext();
        PluginMenuInitOkListener pluginInitOkListener =  new PluginMenuInitOkListener() {
            @Override
            public void initOk(KuaiyanMenuSdkInterface kySdkInterface) {
                initComplate(kySdkInterface,kuaiyanSdkInitListener);
            }

            @Override
            public void initError(KuaiyanMenuSdkInterface kySdkInterface, String msg) {
                initComplate(kySdkInterface,kuaiyanSdkInitListener);
            }
            @Override
            public SDKConfig getConfig() {
                return sdkConfig;
            }
            private void initComplate(KuaiyanMenuSdkInterface kySdkInterface,KuaiyanSdkInitListener kuaiyanSdkInitListener){
                LogUtils.getNewInstance("KyInterfacePull").dob("初始化完成");
                KuaiyanMenuWealSdk.getInstance().initKYSdkInterface(kySdkInterface);
                if(!TextUtils.isEmpty(SdkInit.appKeys)){
                    KuaiyanSdk.getInstance().setAppKye(SdkInit.appKeys);
                }
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitFinished();
                }
            }
        };
        LocalPluginMager.getInstance().copyAssertPlugin(name);//对比assert目录，本地插件目录以及网络下载目录
        if(!TextUtils.isEmpty(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath)) {
            DLPluginManager.getInstance(sdkContext).loadApk(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath);
            KyInterfacePull.getInstance().init(name,pluginInitOkListener);
        }
        checkUpdate(context,name);
    }


    public static void checkUpdate(final Context context, final String name){
        boolean serversRun = false;
        try{
            serversRun = isServiceRunning(context,PluginService.class.getName());
        }catch (Exception e){
            serversRun = false;
        }
        try {
            if(!serversRun) {
                Intent intent = new Intent(context, PluginService.class);
                context.startService(intent);
            }
            uiHander.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(PluginService.Action);
                    intent.putExtra("apkName",name);
                    context.sendBroadcast(intent);
                }
            },10);
        }catch (Exception e){
            LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("启动服务出错"+e.getMessage());
        }
    }

    public static boolean isServiceRunning(Context context, String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (serviceList == null||!(serviceList.size() > 0)) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className)) {
                isRunning = true;
                break;
            }
        }
        return isRunning;
    }

    public static void initKuaiyanDataStatisticsSdk(Context context, final SDKConfig sdkConfig, final KuaiyanSdkInitListener kuaiyanSdkInitListener){
        MenifestChecker.checkMenifest(context);
        String name = ApkNames.dataStatisticsSdk;
        if(ApkNames.inited(name)){
            return;
        }
        ApkNames.inited.put(name,true);
        SdkInit.sdkConfig = sdkConfig;
        if(sdkConfig!=null){
            appKeys = sdkConfig.getAppKey();
        }
        sdkContext = context.getApplicationContext();
        PluginDataStatisticsInitOkListener pluginDataStatisticsInitOkListener = new PluginDataStatisticsInitOkListener() {
            @Override
            public void initOk(KuaiyanDataStatisticsInterface kuaiyanDataStatisticsInterface) {
                initComplate(kuaiyanDataStatisticsInterface, kuaiyanSdkInitListener);
            }

            @Override
            public void initError(KuaiyanDataStatisticsInterface kuaiyanDataStatisticsInterface, String msg) {
                if(kuaiyanDataStatisticsInterface!=null) {
                    initComplate(kuaiyanDataStatisticsInterface, kuaiyanSdkInitListener);
                }else{
                    if(kuaiyanSdkInitListener!=null){
                        kuaiyanSdkInitListener.onCoreInitError(msg);
                    }
                }
            }
            private void initComplate(KuaiyanDataStatisticsInterface kuaiyanDataStatisticsInterface,KuaiyanSdkInitListener kuaiyanSdkInitListener){
                LogUtils.getNewInstance("KyInterfacePull").dob("初始化完成");
                KuaiyanDataStatisticsSdk.getInstance().initKYDataStatisticsSdkInterface(kuaiyanDataStatisticsInterface);
                if(!TextUtils.isEmpty(SdkInit.appKeys)){
                    KuaiyanSdk.getInstance().setAppKye(SdkInit.appKeys);
                }
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitFinished();
                }
            }
            @Override
            public SDKConfig getConfig() {
                return sdkConfig;
            }
        };
        LocalPluginMager.getInstance().copyAssertPlugin(name);//对比assert目录，本地插件目录以及网络下载目录
        if(!TextUtils.isEmpty(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath)) {
            DLPluginManager.getInstance(sdkContext).loadApk(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath);
            KyInterfacePull.getInstance().init(name,pluginDataStatisticsInitOkListener);
        }
        checkUpdate(context,name);
    }

    public static void initKuaiyanAppDataStatisticsSdk(Context context, final SDKConfig sdkConfig, final KuaiyanSdkInitListener kuaiyanSdkInitListener){
        MenifestChecker.checkMenifest(context);
        String name = ApkNames.appDataStatisticsSdk;
        if(ApkNames.inited(name)){
            return;
        }
        ApkNames.inited.put(name,true);
        SdkInit.sdkConfig = sdkConfig;
        if(sdkConfig!=null){
            appKeys = sdkConfig.getAppKey();
        }
        sdkContext = context.getApplicationContext();
        PluginAppDataStatisticsInitOkListener pluginDataStatisticsInitOkListener = new PluginAppDataStatisticsInitOkListener() {
            @Override
            public void initOk(KuaiyanAppDataStatisticsInterface kuaiyanDataStatisticsInterface) {
                initComplate(kuaiyanDataStatisticsInterface, kuaiyanSdkInitListener);
            }

            @Override
            public void initError(KuaiyanAppDataStatisticsInterface kuaiyanDataStatisticsInterface, String msg) {
                if(kuaiyanDataStatisticsInterface!=null) {
                    initComplate(kuaiyanDataStatisticsInterface, kuaiyanSdkInitListener);
                }else{
                    if(kuaiyanSdkInitListener!=null){
                        kuaiyanSdkInitListener.onCoreInitError(msg);
                    }
                }
            }
            private void initComplate(KuaiyanAppDataStatisticsInterface kuaiyanDataStatisticsInterface,KuaiyanSdkInitListener kuaiyanSdkInitListener){
                LogUtils.getNewInstance("KyInterfacePull").dob("初始化完成");
                KuaiyanAppDataStatisticsSdk.getInstance().initKYDataStatisticsSdkInterface(kuaiyanDataStatisticsInterface);
                if(!TextUtils.isEmpty(SdkInit.appKeys)){
                    KuaiyanSdk.getInstance().setAppKye(SdkInit.appKeys);
                }
                if(kuaiyanSdkInitListener!=null){
                    kuaiyanSdkInitListener.onCoreInitFinished();
                }
            }
            @Override
            public SDKConfig getConfig() {
                return sdkConfig;
            }
        };
        LocalPluginMager.getInstance().copyAssertPlugin(name);//对比assert目录，本地插件目录以及网络下载目录
        if(!TextUtils.isEmpty(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath)) {
            DLPluginManager.getInstance(sdkContext).loadApk(LocalPluginMager.getInstance().getLocalPluginBean(name).installPath);
            KyInterfacePull.getInstance().init(name,pluginDataStatisticsInitOkListener);
        }
        checkUpdate(context,name);
    }
}
